#!/usr/bin/env bash
set -e
SCRIPT_DIR=$(cd "$(dirname "$0")" || exit;pwd)

if [ $# -eq 0 ]; then
    echo "usage: [env:CLANG_PATH] [env:TIDY_FLAGS] lint-cc [DIR] [BUILD_DIR]"
    exit 1
fi

BUILD_DIR="build"
if [ -z "$TIDY_FLAGS" ]; then
    TIDY_FLAGS="-quiet"
fi

if [ -n "$CLANG_PATH" ]; then
    PATH=$CLANG_PATH:$PATH
fi

if [ -n "$2" ]; then
    BUILD_DIR=$2
fi

if [ ! -f $BUILD_DIR/compile_commands.json ]; then
    $SCRIPT_DIR/configure --build $BUILD_DIR
fi

which clang-tidy
clang-tidy --version
$SCRIPT_DIR/run-clang-tidy -p $BUILD_DIR $TIDY_FLAGS $1
